// import { getAppid, loginByCode } from "@/apis/user";
// import { useAppStore } from "@/stores/app";
// import { decryptData } from "@/utils/crypto";
// import { parse, stringify } from "@/utils/qs";
import { onLoad } from "@dcloudio/uni-app";
// import qs from 'qs'
import { ref } from "vue";
export interface DefaultForm {
    account: string,
    password: string,
    againPassword: string,
    comCode: string,
    openid: string,
    mobile: string,
    captcha: string,
    newpassword: string,
    username: string,
}
const defaultForm: DefaultForm = {
    account: '',
    password: '',
    againPassword: '',
    comCode: '',
    openid: '',
    mobile: '',
    newpassword: '',
    captcha: '',
    username: '',
}
export function useLoad({ handleClick }) {
    // const appstore = useAppStore();
    const appstore = {};
    const form = ref<DefaultForm>(defaultForm)
    const logined = ref(false)
    const currentModeIndex = ref<0 | 1 | 2>(0)
    onLoad(async (options: any) => {
        if (options.logined) {
            logined.value = true
            currentModeIndex.value = 2
            return
        }
        // #ifdef H5
        let params = options;
        params.comCode 
        if (params.comCode) {
            params.comCode = params.comCode;
        }
        if (appstore.automaticLogin === 2) return
        const reslust = await getAppid({ comcode: params.comCode ?? 'wechat' });
        appstore.appid = reslust.data.appid;

        if (!params.state && params.comCode) {

            try {
                setTimeout(() => {
                    uni.hideToast();
                    uni.hideLoading();
                });
                let setParams = Object.assign(reslust.data, params);
                if (params.query) {
                    appstore.share_params = params.query
                }
                const state = encodeURIComponent(stringify({
                    comCode: setParams.comCode,
                    isShare: params.query ? 1 : 2,
                }));
                const wechatCall = `https://open.weixin.qq.com/connect/oauth2/authorize?
						appid=${setParams.appid}&redirect_uri=${encodeURIComponent(
                    window.location.origin + "/pagesA/login/index"
                )}&response_type=code&state=${state}&scope=snsapi_userinfo`;
                window.location.href = wechatCall;
            } catch (error) {
                uni.reLaunch({ url: "/pagesA/login/index" });
            }
        }
        // #endif
        if (!options.state) return;
        if (options.state) {
            let setParams = parse(decodeURIComponent(options.state));
            try {
                const relust = await loginByCode({
                    comcode: setParams.comCode,
                    code: options.code,
                });
                //  如果是分享点击进来的，则直接登录
                console.log('share_paramsshare_params', appstore.share_params)
                if (appstore.share_params && setParams.isShare == 1) {
                    const decryptedData = decryptData(decodeURIComponent(appstore.share_params), appstore.salt, appstore.pass);
                    console.log("Decrypted:", decryptedData);
                    let query = parse(decryptedData);
                    console.log("query:", query);
                    appstore.share_params = '';
                    if (query.share && query.username && query.password) {
                        form.value.account = query.username;
                        form.value.password = query.password;
                        form.value.openid = relust.data?.openid;
                        form.value.comcode = setParams.comCode;
                        handleClick();
                        return;
                    }
                }
                //  否则，判断是否已经登录了
                const userinfo = relust.data?.userinfo;
                setParams.openid = relust.data?.openid;
                appstore.base_params = setParams
                if (userinfo) {
                    appstore.token = userinfo.token
                    appstore.userInfo = userinfo
                    uni.reLaunch({ url: "/pages/index/index" });
                }
            } catch (error) {
                console.log('error', error)
                uni.$u.toast(error.msg || "未知错误");
            }
        }
    })
    return { form, defaultForm, appstore, logined, currentModeIndex }

}